package com.dongdong.controller;


import com.alibaba.fastjson.JSONObject;
import com.dongdong.model.TbAddress;
import com.dongdong.result.Result;
import com.dongdong.result.ResultConstant;
import com.dongdong.service.AddressService;
import com.dongdong.utils.JwtUtil;
import com.dongdong.vo.AddressVO;

import com.dongdong.vo.UserVO;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("address")
public class AddressController {

    @Autowired
    private AddressService addressService;
    @Autowired
    private JwtUtil jwtUtil;


    @GetMapping("/selectAddressByUsername")
    public Result selectAddressByUsername(@RequestHeader String token){

        //解析token
        Claims claims = jwtUtil.parseJwt(token);
        String subject = claims.getSubject();
        UserVO userVO = JSONObject.parseObject(subject, UserVO.class);


        List<AddressVO> addressVOS= addressService.selectAddressByUsername(userVO.getUsername());
        return Result.OK().setData(addressVOS);

    }

    //通过id查询地址信息
     @GetMapping("/selectAddrById")
     public TbAddress selectAddrById(@RequestParam Long id){
            return addressService.selectAddrById(id);
        }



    //增加
    @PostMapping("add")
    public int add(@RequestBody AddressVO addressVO){
        return addressService.insert(addressVO);
    }

}
